/*
 * Copyright 2009 BioTeam Inc
 * 
 * Licensed under License GNU LESSER GENERAL PUBLIC LICENSE version 3.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.bioteam.appweb.gwt.apps.client.user;

import net.bioteam.appweb.gwt.apps.client.StaticHolder;
import net.bioteam.common.user.UserInfo;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.EnterKeyEditAction;
import com.smartgwt.client.types.ListGridEditEvent;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.types.RowEndEditAction;
import com.smartgwt.client.widgets.form.validator.CustomValidator;
import com.smartgwt.client.widgets.form.validator.LengthRangeValidator;
import com.smartgwt.client.widgets.form.validator.RegExpValidator;
import com.smartgwt.client.widgets.form.validator.Validator;
import com.smartgwt.client.widgets.grid.CellFormatter;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;

public class UsersGrid extends ListGrid
{
	public UsersGrid()
	{
		setWidth(500);
		setHeight100();
		setAlternateRecordStyles(true);
		setShowAllRecords(true);
		setShowEmptyMessage(true);
		//setEmptyMessage("No User is loaded yet");
		
		setCanEdit(true);  
		setEditEvent(ListGridEditEvent.DOUBLECLICK);  
		setListEndEditAction(RowEndEditAction.DONE);  
		setModalEditing(true); 
		setAutoSaveEdits(true);
		setEnterKeyEditAction(EnterKeyEditAction.NEXTCELL);
		setRowEndEditAction(RowEndEditAction.DONE);
		
		setDataSource(new UsersDataSource());
		setShowFilterEditor(true);
		setFilterOnKeypress(true); 
		
		
		ListGridField nameField = new ListGridField("name", "Name", 80);
		nameField.setType(ListGridFieldType.TEXT);
		nameField.setCanEdit(true);
		nameField.setRequired(true);
		Validator validator=new CustomValidator()
		{

			@Override
			protected boolean condition(Object value)
			{
				String str=value.toString();
				if (str.length()<6)
				{
					setErrorMessage("must have 6 characters at minimal");
					return false;
				}
				if (!str.matches("^[a-zA-Z].+"))
				{
					setErrorMessage("First character has to be a letter");
					return false;
				}
				if (!str.matches("(\\w|\\d)+"))
				{
					setErrorMessage("all characters have to be letter or number");
					return false;
				}
				return true;
			}
		};
		nameField.setValidators(validator);
		

		LengthRangeValidator lrv=new LengthRangeValidator();
		lrv.setMin(new Integer(6));
		ListGridField passwordField = new ListGridField("password", "Password", 80);
		//passwordField.setType(ListGridFieldType.)
		passwordField.setValidators(lrv);
		passwordField.setRequired(true);
		passwordField.setCanFilter(false);
		passwordField.setCellFormatter(new CellFormatter(){

			public String format(Object value, ListGridRecord record, int rowNum, int colNum)
			{
				return "***";
			}});
		
		
		ListGridField roleField = new ListGridField("role", "Role", 50);
		
		RegExpValidator regV=new RegExpValidator();
		regV.setExpression(".+@.+");
		regV.setErrorMessage("invalid email address");
		ListGridField emailField = new ListGridField("email", "Email", 100);
		emailField.setValidators(regV);
		emailField.setRequired(true);
		
		ListGridField activeField=new ListGridField("active", "Active", 50);
		activeField.setCanFilter(true);
		activeField.setCanToggle(true);
		
		ListGridField quotaField=new ListGridField("dailyquote", "Daily Job", 50);
		quotaField.setCanEdit(true);
		quotaField.setCellFormatter(new CellFormatter(){

			public String format(Object value, ListGridRecord record, int rowNum, int colNum)
			{
				if (value==null)
					return "";
				if(value.toString().equals(String.valueOf(UserInfo.UNLIMITEDQUOTE)))
				{
					return "UNLIMITED";
				} else
				{
					return value.toString();
				}
			}});
		
		ListGridField ec2Field=new ListGridField("ec2user", "EC2 User", 60);
		ec2Field.setType(ListGridFieldType.BOOLEAN);
		ec2Field.setPrompt("Does the user have EC2 access info?");
		ec2Field.setAlign(Alignment.CENTER);
		ec2Field.setCanEdit(false);
		ec2Field.setCellFormatter(new CellFormatter(){

			public String format(Object value, ListGridRecord record, int rowNum, int colNum)
			{
				if (value!=null && (Boolean)value)
				{
					return "<span class='red'>Y</span>";
					
				} else
				{
					return "N";
				}
			}});
		
		setFields(nameField, passwordField, roleField, emailField, activeField, quotaField, ec2Field);
		
	}

	
	//Criteria criteria=new Criteria();
	
	
	//http://code.google.com/p/smartgwt/issues/detail?id=105. this is fixed.
	@Override
	public boolean canEditCell(int rowNum, int colNum)
	{
		if (colNum==0 && this.getRecord(rowNum)!=null)
		{
			return false;
		}
		
		return super.canEditCell(rowNum, colNum);
	}
	
	boolean loaded = false;

	public void load()
	{
		if (loaded)
			return;
		StaticHolder.setStatusMessage("loading users ...", 3);
		fetchData();
		loaded = true;
	}

	public void reload()
	{
		invalidateCache();
		loaded=false;
		load();
	}
	
	
}
